package com.example.dingzexi_homework1;
import android.widget.TextView;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CreateFragment extends Fragment {

    public CreateFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_create, container, false);

        EditText editTextContent = view.findViewById(R.id.editTextContent);
        Button buttonPublish = view.findViewById(R.id.buttonPublish);
        LinearLayout linearLayoutContent = view.findViewById(R.id.linearLayoutContent);

        buttonPublish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = editTextContent.getText().toString();
                if (!content.isEmpty()) {
                    // 将内容添加到创作页面的下面
                    addContentToLayout(linearLayoutContent, content);
                    // 清空输入框
                    editTextContent.setText("");
                }
            }
        });

        return view;
    }
    private void addContentToLayout(LinearLayout linearLayout, String content) {
        // 创建一个新的TextView来显示内容
        TextView textView = new TextView(getContext());
        textView.setTextSize(16);

        // 获取当前时间并格式化
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", java.util.Locale.getDefault());
        String formattedDate = dateFormat.format(new Date());

        // 将时间戳添加到内容的前面
        String displayContent = formattedDate + "\n" + content;

        textView.setText(displayContent);
        textView.setPadding(16, 8, 16, 8);
        // 将TextView添加到LinearLayout中
        linearLayout.addView(textView);
    }
}